package games;

public class ErikHeuristic extends KamisadoState {

	private int GOALPOSITIONPOINTS = 20;
	
	// False playing from top
	@Override
	protected int heuristic(boolean player) {
		int yModifier = 1;
		if(!player)
			yModifier = -1;
		
		int positionPoints = calculatePositionPoints(player);
		int loosingColor = notLoosingColor(player);
		
		return positionPoints * loosingColor * yModifier;
	}
	
	// Public for junit purpose
	public int calculatePositionPoints(boolean player) {
		int posPoints = 0;
		int goalPoints = 0;
		Coordinate c = super.towerPositions.get(new Tower(SquareColor.BLUE, player));
		posPoints += getY(c.Y, player);
		goalPoints += (nbrOfGoalPositionsForTower(c.X, c.Y, player) * GOALPOSITIONPOINTS);
		c = super.towerPositions.get(new Tower(SquareColor.BROWN, player));
		posPoints += getY(c.Y, player);
		goalPoints += (nbrOfGoalPositionsForTower(c.X, c.Y, player) * GOALPOSITIONPOINTS);
		c = super.towerPositions.get(new Tower(SquareColor.GREEN, player));
		posPoints += getY(c.Y, player);
		goalPoints += (nbrOfGoalPositionsForTower(c.X, c.Y, player) * GOALPOSITIONPOINTS);
		c = super.towerPositions.get(new Tower(SquareColor.ORANGE, player));
		posPoints += getY(c.Y, player);
		goalPoints += (nbrOfGoalPositionsForTower(c.X, c.Y, player) * GOALPOSITIONPOINTS);
		c = super.towerPositions.get(new Tower(SquareColor.PINK, player));
		posPoints += getY(c.Y, player);
		goalPoints += (nbrOfGoalPositionsForTower(c.X, c.Y, player) * GOALPOSITIONPOINTS);
		c = super.towerPositions.get(new Tower(SquareColor.PURPLE, player));
		posPoints += getY(c.Y, player);
		goalPoints += (nbrOfGoalPositionsForTower(c.X, c.Y, player) * GOALPOSITIONPOINTS);
		c = super.towerPositions.get(new Tower(SquareColor.RED, player));
		posPoints += getY(c.Y, player);
		goalPoints += (nbrOfGoalPositionsForTower(c.X, c.Y, player) * GOALPOSITIONPOINTS);
		c = super.towerPositions.get(new Tower(SquareColor.YELLOW, player));
		posPoints += getY(c.Y, player);
		goalPoints += (nbrOfGoalPositionsForTower(c.X, c.Y, player) * GOALPOSITIONPOINTS);
		
		return posPoints + goalPoints;
	}
	
	// Public for junit purpose
	public int notLoosingColor(boolean player) {
		KamisadoMove prev = super.getPreviousMove();
		SquareColor color = gameBoard[prev.getY()][prev.getX()].getColor();
		Coordinate c = getTowerPosition(color, !prev.getTower().getPlayer());
		if(nbrOfGoalPositionsForTower(c.X, c.Y, !prev.getTower().getPlayer()) > 0)
			return 0;
		
		return 1;
	}

	private int nbrOfGoalPositionsForTower(int x, int y, boolean player) {
		int yModifier = 1;
		if(!player)
			yModifier = -1;
		
		// Check straight
		int positions = 3;
		int tmpY = y+yModifier;
		int xLeft = x-1;
		int xRight = x+1;
		boolean checkLeft = true, checkRight = true, checkStraight = true;
		
		while(tmpY >= 0 && tmpY < 8) {
			if(gameBoard[tmpY][x].getTower() != null && checkStraight) {
				positions--;
				checkStraight = false;
			}
			if((xLeft < 0 || gameBoard[tmpY][xLeft].getTower() != null) && checkLeft) {
				positions--;
				checkLeft = false;
			}
			if((xRight >= 8 || gameBoard[tmpY][xRight].getTower() != null) && checkRight) {
				positions--;
				checkRight = false;
			}
			xLeft--;
			xRight++;
			tmpY += yModifier;
		}
		return positions;
	}
	
	private Coordinate getTowerPosition(SquareColor color, boolean player) {
		return super.towerPositions.get(new Tower(color, player));
	}
	
	private int getY(int y, boolean player) {
		if(!player)
			return (y-7)*-1;
		
		return y;
	}
	
}
